package com.wzt.org.web.controller;

import com.wzt.base.util.AjaxResponse;
import com.wzt.base.util.PageResult;
import com.wzt.org.domain.Department;
import com.wzt.org.query.DepartmentQuery;
import com.wzt.org.query.Query;
import com.wzt.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Zenith
 */
@RestController
@RequestMapping("/department")
@Api(value = "部门的API", tags = "部门的CRUD")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @ApiOperation(value = "部门的新增或修改" )
    @PutMapping
    public AjaxResponse save(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.addOne(department);
            } else {
                departmentService.editOne(department);
            }
            return AjaxResponse.oneResponse().setMessage("保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("保存失败！");
        }
    }

    @ApiOperation(value = "通过id删除部门" )
    @DeleteMapping("/{id}")
    public AjaxResponse deleteOne(@PathVariable Long id) {
        try {
            departmentService.removeOne(id);
            return AjaxResponse.oneResponse().setMessage("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("删除失败！");
        }
    }

    @ApiOperation(value = "批量删除部门" )
    @PatchMapping
    public AjaxResponse batchDelete(@RequestBody List<Long> ids) {
        try {
            departmentService.removeMul(ids);
            return AjaxResponse.oneResponse().setMessage("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("删除失败！");
        }
    }

    @ApiOperation(value = "通过ID获取部门" )
    @GetMapping("/{id}")
    public AjaxResponse getOne(@PathVariable Long id) {
        try {
            Department department = departmentService.getOne(id);
            return AjaxResponse.oneResponse().setMessage("获取成功！").setData(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取失败！");
        }
    }

    @ApiOperation(value = "获取所有的部门" )
    @GetMapping
    public AjaxResponse getList() {
        try {
            List<Department> departments = departmentService.getAll();
            return AjaxResponse.oneResponse().setMessage("获取成功！").setData(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取失败！");
        }
    }

    @ApiOperation(value = "获取部门的页面" )
    @PostMapping
    public AjaxResponse getPage(@RequestBody DepartmentQuery query) {
        try {
            PageResult<Department> page = departmentService.getPage(query);
            return AjaxResponse.oneResponse().setMessage("获取成功！").setData(page);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取失败!");
    }
}
